#!/usr/bin/env perl

#
# Copyright (c) 2002-2004 Eric Wallengren
# This file is part of the Continuous Automated Build and Integration
# Environment (CABIE)
#
# CABIE is distributed under the terms of the GNU General Public
# License version 2 or any later version.  See the file COPYING for copying
# permission or http://www.gnu.org.
#
# THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED OR
# IMPLIED, without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  ANY USE IS AT YOUR OWN RISK.
#
# Permission to modify the code and to distribute modified code is granted,
# provided the above notices are retained, and a notice that the code was
# modified is included with the above copyright notice.
#

BEGIN {
    push @INC, "lib";
    push @INC, "../lib";
    push @INC, "../../lib";
}

use Getopt::Long;
use English;
use File::Basename;
use Sys::Hostname;

my $hostname = hostname();
$hostname    =~ s/\.[a-zA-Z0-9\n]+//g;
$hostname    =~ s/-//g;

require "$hostname.pm";

my $config = new $hostname;

my $logo = $config->LOGOICON;

use vars qw(
    $opt_infile
    $opt_outfile
    $opt_errorfile
    $opt_help);


ParseArgs();

my $lines;

my @errorarray = ReadFile($opt_errorfile);
my @document = ProcessFile($opt_infile, $opt_outfile);

open (OUT, ">$opt_outfile") || die "open: $?";

foreach $line (reverse @document) {
    print OUT "$line";
}

close(OUT) || die "close: $?";


#-------------------------------------------------------------------------
sub Usage {

    my ( $program ) = basename( $PROGRAM_NAME );

    print << "EOF"
Usage: $program --infile infile --outfile outfile --errorfile errorfile 

       Required args:

           --infile    | -i    = input file (build log)
           --outfile   | -o    = output file (html file)
           --errorfile | -e    = error file (list file)

       Optional args:

           --help      | -h    = help
EOF

}

sub ParseArgs {

    $Getopt::Long::ignorecase = 0;

    if ( ! GetOptions( "infile|i=s",
                       "outfile|o=s",
                       "errorfile|e=s",
                       "help|h",
                        ) )

    {
        Usage;
        exit 1;
    }

    if ( $opt_help ) {
        Usage;
        exit 0;
    }

    if (!$opt_infile) {
        Usage;
        exit 1;
    }

    if (!$opt_outfile) {
        Usage;
        exit 1;
    }

    if (!$opt_errorfile) {
        Usage;
        exit 1;
    }

}

sub ReadFile {

    my $File = shift;
    my @contents;

    if (! -f $File) {

        print "no file: $File\n";
        exit -1;

    } else {

        open (EF, "<$File") || die "open: $?";
        @contents = <EF>;
        close(EF) || die "close: $?";

    }

    return @contents;

}


sub ProcessFile {

    my $in  = shift;
    my $out = shift;

    my $entry;
    my $search;
    my $i;

    my @inarray;

    my @reversearray;

    my $errors   = 0;
    my $warnings = 0;

    if (! -f $in) {
        print "no file: $in\n";
        exit -1;
    } else {
        open(IN, "<$in") || die "open: $?";
        @inarray = <IN>;
        close(IN) || die "close: $?";
    }

    push @reversearray, "</pre></body></html>\n";

    foreach $search (reverse @inarray) {

        $Error = 0;

        chomp $search;

        foreach $entry (@errorarray) {

            chomp $entry;

            my $x = index($search, $entry);

            if ($x > -1) {
                $Error = 1;
                $errors++;
            }

        }

        my $w = index($search, "warning");

        if ($Error) {
            push @reversearray, "</pre><hr><pre>\n";
            push @reversearray, "<a name=e_$errors></a><font color=\"#FE5066\">$search</font>\n";
            push @reversearray, "</pre><hr><pre>\n";
        } else {
            if ($w != -1) {
                $warnings++;
                push @reversearray, "</pre><hr><pre>\n";
                push @reversearray, "<a name=w_$warnings></a><font color=\"#ff6600\">$search</font>\n";
                push @reversearray, "</pre><hr><pre>\n";
            } else {
                push @reversearray, "<font color=\"#000000\">$search</font>\n";
            }
        }

    }

    push @reversearray, "<pre>\n";
    push @reversearray, "<hr>\n";
    
    push @reversearray, "</ul>";
    for ($i = $warnings; $i > 0; $i--) {
        push @reversearray, "<li><a href=\"#w_$i\">Warning $i</a>\n";
    }
    for ($i = $errors; $i > 0; $i--) {
        push @reversearray, "<li><a href=\"#e_$i\">Error $i</a>\n";
    }
    push @reversearray, "<ul>";

    push @reversearray, "</table>\n";
    push @reversearray, "</tr>\n";
    push @reversearray, "</td>\n";
    push @reversearray, "</font>\n";
    push @reversearray, "<h2>A-BIMS - Build Log&nbsp;</h2>\n";
    push @reversearray, "<font color=#000077>\n";
    push @reversearray, "<td valign=\"bottom\" align=\"right\">\n";
    push @reversearray, "</td>\n";
    push @reversearray, "<img src=\"$logo\" alt=\"LOGO\" align=middle>\n";
    push @reversearray, "<td rowspan='2'>\n";
    push @reversearray, "<tr>\n";
    push @reversearray, "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
    push @reversearray, "<body>\n";
    push @reversearray, "</head>\n";
    push @reversearray, "<head>\n";
    push @reversearray, "<html>\n";

    return @reversearray;
}
